package com.dingwei.superiordiary.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.adapter.BlackListlAdapter;
import com.dingwei.superiordiary.bean.BlackBean;
import com.dingwei.superiordiary.mvp.presenter.BlackPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author 南柯一梦丶
 * Created time 2018/10/25.
 * Personal signature 余生还长 何必慌张
 * Page description :
 * .::::.
 * .::::::::.
 * :::::::::::
 * ..:::::::::::'
 * '::::::::::::'
 * .::::::::::
 * '::::::::::::::..
 * ..::::::::::::.
 * ``::::::::::::::::
 * ::::``:::::::::'        .:::.
 * ::::'   ':::::'       .::::::::.
 * .::::'      ::::     .:::::::'::::.
 * .:::'       :::::  .:::::::::' ':::::.
 * .::'        :::::.:::::::::'      ':::::.
 * .::'         ::::::::::::::'         ``::::.
 * ...:::           ::::::::::::'              ``::.
 * ```` ':.          ':::::::::'                  ::::..
 * '.:::::'                    ':'````..
 */
public class BlackListActivity extends BaseActivity implements Contract.BlackView {
    BlackPresenter blackPresenter = new BlackPresenter(this);
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    @Bind(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;

    BlackListlAdapter adapter;

    int page = 1;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_black_list;
    }

    @Override
    protected void initData() {
        refreshLayout.autoRefresh();
    }

    @Override
    protected void initView() {
        tvTitle.setText("黑名单");

        adapter = new BlackListlAdapter(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.addItemDecoration(new DividerDecoration(getResources().getColor(R.color.line), 1));
        mRecyclerView.setAdapter(adapter);


    }

    @Override
    protected void initListener() {

        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshLayout) {
                page = page + 1;
                blackPresenter.initData(page, false, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                page = 1;
                blackPresenter.initData(page, true, false);

            }
        });

        adapter.attentionInterfaceListener(new BlackListlAdapter.attentionInterFace() {
            @Override
            public void remove(int position) {
                BlackBean.DataBean.ListBean bean = adapter.getItem(position);
                blackPresenter.removeBlackList(bean.attention_id, position);
            }

            @Override
            public void look(int position) {
                BlackBean.DataBean.ListBean bean = adapter.getItem(position);
                blackPresenter.blackStateSave("1", bean.attention_id, position);
            }

            @Override
            public void replay(int position) {
                BlackBean.DataBean.ListBean bean = adapter.getItem(position);
                blackPresenter.blackStateSave("2", bean.attention_id, position);
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onFail() {
        showData("fail");
    }

    @Override
    public void onLoadMore(BlackBean bean) {
        adapter.addAll(bean.data.list);
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadmore();
        showData("success");
    }

    @Override
    public void onRefresh(BlackBean bean) {

        adapter.clear();
        adapter.addAll(bean.data.list);

        refreshLayout.finishRefresh();
        refreshLayout.finishLoadmore();
        showData("success");
    }

    @Override
    public void blackStateSaveSuccess(String type, int position) {
        BlackBean.DataBean.ListBean bean = adapter.getItem(position);
        if (type.equals("1")) {
            if (bean.you_show.equals("1")) {
                bean.you_show = "0";
            } else {
                bean.you_show = "1";
            }
        }
        if (type.equals("2")) {
            if (bean.replay_me.equals("1")) {
                bean.replay_me = "0";
            } else {
                bean.replay_me = "1";
            }
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void removeBlackListSuccess(int position) {
        adapter.remove(position);
        adapter.notifyDataSetChanged();
    }

    public void showData(String state) {
        if (state.equals("success")) {
            refreshLayout.setVisibility(View.VISIBLE);
            noDataLayout.setVisibility(View.GONE);
        } else {
            refreshLayout.setVisibility(View.GONE);
            noDataLayout.setVisibility(View.VISIBLE);
        }

    }

    @OnClick(R.id.img_back)
    public void onViewClicked() {
        finish();
    }
}
